<template>
  <div class="app-container">
    <el-form @submit.native.prevent :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px" :label-position="labelPosition">
      <el-form-item label="仓库" prop="spckmc">
        <el-select
            v-model="queryParams.spckid"
            placeholder="请选择"
            clearable
            @change="changeWarehouse"
            filterable
        >
            <el-option
                v-for="item in warehouseList"
                :key="item.spckid"
                :label="item.spckmc"
                :value="item.spckid"
            />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="getList">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      </el-form>
      <el-table
        v-loading="loading"
        :data="customerCategoryWarehouseList"
        show-summary
        :summary-method="getSummaries"
      >
        <af-table-column prop="ssrqid" label="日期" fixed="left"></af-table-column>
        <af-table-column prop="spckmc" label="仓库"></af-table-column>
        <af-table-column prop="spjs" label="结余件数"></af-table-column>
        <af-table-column prop="spsl" label="结余重量(吨)"></af-table-column>
      </el-table>
      <pagination
          v-show="total>0"
          :total="total"
          :page.sync="pageNum"
          :limit.sync="pageSize"
          @pagination="getList"
        />
  </div>
</template>

<script>
import { getListDaysCategory } from "@/api/storage/dataAnalysis/customerCategory";
import { selectWarehouse17 } from "@/api/storage/warehouse"
import { NUMBER_DIDGIT, WEIGHT_DIGIT } from '@/utils/constant'

export default {
  name: "CustomerCategoryWarehouse",
  data() {
    return {
      // 显示搜索条件
      showSearch: true,
      //对齐
      labelPosition: "right",
      //加载仓库
      selectWarehouse17Loading: true,
      //列表加载
      loading: true,
      // 查询参数
      queryParams: {
        khmcid: "",
        ssrqid1: "",
        ssrqid2: "",
        spckid: "",
      },
      // 按仓区分
      customerCategoryWarehouseList: [],
      //仓库列表
      warehouseList:[],
      //页数
      pageSize: 10,
      //页码
      pageNum:1,
      // 总条数
      total: 0,
    };
  },
  beforeRouteEnter(to, from, next) {
    const query = to.query
    next(vm => {   
      if(Object.keys(query).length > 0) {
        vm.queryParams.khmcid = query.khid
        vm.getList()
        vm.getWarehouse()
      }
    })
  },
  methods: {
     /**获取仓库 仓库不一定是这个*/
     getWarehouse() {
      this.selectWarehouse17Loading = true
      selectWarehouse17({}).then(response => {
        this.warehouseList = response.data
        this.selectWarehouse17Loading = false
      });
    },

    /** 查询按仓区分 */
    getList() {
      this.loading = true
      getListDaysCategory(this.queryParams, {pageNum:this.pageNum, pageSize:this.pageSize}).then(response => {
        this.customerCategoryWarehouseList = response.rows
        this.total = response.total
        this.loading = false
      });
    },

     /**选择仓库 */
     changeWarehouse(id) {
      this.getList()
    },

    //合计
    getSummaries({ columns, data }) {
      const sums = []
      columns.forEach((column, index) => {
        if(index === 0) {
          sums[index] = "合计"
			    return
        }
        if ((column.property === "spjs" || column.property === "spsl")) {
          const values = data.map((item) => {
            return parseFloat(this.delcommafy(item[column.property]))
          });
          sums[index] = values.reduce((prev, curr) => {
            if(column.property === "spsl") {
              return parseFloat((prev + curr).toFixed(WEIGHT_DIGIT))
            }
            if(column.property === "spjs") {
              return parseFloat((prev + curr).toFixed(NUMBER_DIDGIT))
            }
          }, 0);
        }
      })
      return sums
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.reset()
      this.getList()
    },
    reset() {
      Object.assign(this.$data, this.$options.data())
    },
  }
};
</script>
